package ru.fadeev.crammer.ui.window;


import java.awt.*;
import java.awt.event.*;

import java.util.Vector;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;

import ru.fadeev.crammer.GFIndicesStorage;
import ru.fadeev.crammer.ui.BasicCellRenderer;
import ru.fadeev.crammer.ui.window.TestGapsFillingDialog.TestGFHeapRecord;


/**
 * ViewLosersDialog provides GUI for viewing losers - words that don't have 
 * phrases containing them, so aren't able to take part in Gaps Filling Test
 * @author Konstantin Fadeyev
 *
 */
public class ViewLosersDialog extends JDialog {
	
	// UI Components
	private JPanel mainPanel;
	
	private JScrollPane wordsScrollPane;
	private JTable wordsTable;
    private LosersTableModel wordsTableModel;
	   
	private JPanel bottomPanel;
	private JPanel checkButtonPanel;
	private JButton checkBankButton;
	private JPanel closeButtonPanel;
    private JButton closeButton;

    
    //User fields
    
  	/** 
  	 * Vector containing all words 
  	 * that don't have phrase containing them 
  	 */
  	private Vector<TestGFHeapRecord> losers;  	
    private GFIndicesStorage storage;

    
    /**
     * Constructs ViewLosersDialog
     * @param owner parent window
     * @param losers losers vector to show 
     */
    public ViewLosersDialog(Dialog owner, Vector<TestGFHeapRecord> losers,
    		GFIndicesStorage storage) {
        super(owner,true);
        
        this.losers = losers;
        this.storage = storage;
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(200, 500);
        setLocation((screenSize.width-200)/2, (screenSize.height-500)/3);
        
       	setTitle("Viewing words");
       
        componentsInit();
        placeComponents();
    }
    
    
    /**
     * Initializes UI Components of dialog
     */
    private void componentsInit() {
        mainPanel = new JPanel();
        bottomPanel = new JPanel();
		
		// Words Table
    	wordsTableModel = new LosersTableModel(losers);    
    	wordsTable = new JTable(wordsTableModel);
    	wordsTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	
    	BasicCellRenderer cellRenderer = new BasicCellRenderer();
    	wordsTable.setDefaultRenderer(String.class, cellRenderer);
    	
    	wordsTable.getTableHeader().setReorderingAllowed(false);
           	
        JTextField textField = new JTextField();
        textField.setMargin(new Insets(0,0,0,0));
        textField.setEditable(false);
        textField.setBackground(BasicCellRenderer.selectionColor);
        DefaultCellEditor testEditor = new DefaultCellEditor(textField);
        testEditor.setClickCountToStart(1);
        
        wordsTable.setDefaultEditor(String.class, testEditor);
        
    	wordsScrollPane = new JScrollPane(wordsTable);    	
    	
    	closeButtonPanel = new JPanel();
    	checkButtonPanel = new JPanel();
    	
		ImageIcon checkBankIcon = new ImageIcon("icons/16/checkBankWords.png");
		checkBankButton = new JButton(checkBankIcon);
		checkBankButton.setToolTipText("Look up in Phrases Bank for phrases");     
		checkBankButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
		        		Vector<String> wordVector = new Vector<String>();
			        	for(int i = 0; i < losers.size(); i++) {
			        		TestGFHeapRecord record = 
			        			(TestGFHeapRecord)losers.elementAt(i);
			        		wordVector.addElement(record.word.getWord());
			        	}	
		        		storage.addPHBTask(wordVector);
			        }
			    });
    	
        closeButton = new JButton("Close");
        getRootPane().setDefaultButton(closeButton);
        
        closeButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	setVisible(false);
			        }
			    });
        
        setContentPane(mainPanel);
    }
    
    
    /**
     * Places UI components using different layout managers
     */
    private void placeComponents() {
    	mainPanel.setLayout(new BorderLayout());
    	mainPanel.add(wordsScrollPane, BorderLayout.CENTER);
    	mainPanel.add(bottomPanel, BorderLayout.SOUTH);

    	
    	bottomPanel.setPreferredSize(new Dimension(200, 40));
    	bottomPanel.setLayout(new BorderLayout());
    	bottomPanel.add(checkButtonPanel, BorderLayout.WEST);
    	bottomPanel.add(closeButtonPanel, BorderLayout.EAST);
    	
    	checkButtonPanel.setPreferredSize(new Dimension(30, 30));
    	checkButtonPanel.setLayout(null);
    	checkButtonPanel.add(checkBankButton);
    	checkBankButton.setBounds(10, 10, 20, 20);
    	
    	closeButtonPanel.setPreferredSize(new Dimension(110, 40));
    	closeButtonPanel.setLayout(null);
    	closeButtonPanel.add(closeButton);
    	  	
    	closeButton.setBounds(10, 10, 90, 20);
    }
    
      
    /**
     * LosersTableModel class represents table model
     * for words table in ViewLosersDialog
     * @author Konstantin Fadeyev
     *
     */
    public class LosersTableModel extends AbstractTableModel {

      	/** 
      	 * Vector containing all words 
      	 * that don't have phrase containing them 
      	 */
      	private Vector<TestGFHeapRecord> losers;  	

    	public LosersTableModel(Vector<TestGFHeapRecord> losers) {
    		super();
    		this.losers = losers;
    	}

    	public int getRowCount() {
             return losers.size();
    	}

    	public int getColumnCount() {
    		return 1;
    	}

    	public String getColumnName(int num) {
    		return "Word";
    	}

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

    	public Object getValueAt(int row, int column) {
    		if(row < losers.size()) {
    			TestGFHeapRecord record = losers.elementAt(row);
    			return record.word.getWord();
    		} else {
    			return "";
    		}
    	}

        public Class<?> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
    }
}
